package com.zpark.drug.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpark.drug.mapper.EmployeeMapper;
import com.zpark.drug.pojo.Employee;
import com.zpark.drug.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<Employee> findAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public Employee findById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addEmployee(Employee employee) {
        return employeeMapper.insert(employee);
    }

    @Override
    public int updateEmployee(Employee employee) {
        return employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public int deleteEmployeeById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Employee> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param)) {
            criteria.orLike("name", "%" + param + "%")
                    .orLike("sex", "%" + param + "%")
                    .orLike("address", "%" + param + "%")
                    .orLike("contact", "%" + param + "%");
        }
        return new PageInfo<>(employeeMapper.selectByExample(example));
    }

}
